<template>
  <div class="divBox relative">
    <el-card class="box-card">
      <div class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px">
            <el-form-item :label="$t('dingDanZhuangTai') + '：'" v-if="checkPermi(['admin:order:status:num'])">
              <el-radio-group v-model="tableFrom.refundStatus" type="button" @change="seachList">
                <el-radio-button label="9"
                  >{{ $t('quanBu') }} {{ '(' + orderChartType.all ? orderChartType.all : 0 + ')' }}
                </el-radio-button>
                <el-radio-button label="0"
                  >{{ $t('daiShenHe') }} {{ '(' + orderChartType.await ? orderChartType.await : 0 + ')' }}
                </el-radio-button>
                <el-radio-button label="1"
                  >{{ $t('shenHeWeiTongGuo') }} {{ '(' + orderChartType.reject ? orderChartType.reject : 0 + ')' }}
                </el-radio-button>
                <el-radio-button label="2"
                  >{{ $t('tuiKuanZhong') }} {{ '(' + orderChartType.refunding ? orderChartType.refunding : 0 + ')' }}
                </el-radio-button>
                <el-radio-button label="3"
                  >{{ $t('yiTuiKuan') }} {{ '(' + orderChartType.refunded ? orderChartType.refunded : 0 + ')' }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item :label="$t('shiJianXuanZe') + '：'" class="width100">
              <el-radio-group
                v-model="tableFrom.dateLimit"
                type="button"
                class="mr20"
                size="small"
                @change="selectChange(tableFrom.dateLimit)"
              >
                <el-radio-button v-for="(item, i) in fromList.fromTxt" :key="i" :label="item.val"
                  >{{ item.text }}
                </el-radio-button>
              </el-radio-group>
              <el-date-picker
                v-model="timeVal"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                size="small"
                type="daterange"
                placement="bottom-end"
                :placeholder="$t('ziDingYiShiJian')"
                style="width: 220px"
                @change="onchangeTime"
              />
            </el-form-item>
            <el-form-item :label="$t('dingDanHao') + '：'" class="width100">
              <el-input
                v-model="tableFrom.merOrderNo"
                :placeholder="$t('qingShuRuDingDanHao')"
                class="selWidth"
                size="small"
                clearable
              >
                <el-button slot="append" icon="el-icon-search" size="small" @click="seachList" />
              </el-input>
            </el-form-item>
            <el-form-item :label="$t('tuiKuanDanHao') + '：'" class="width100">
              <el-input
                v-model="tableFrom.refundOrderNo"
                :placeholder="$t('qingShuRuTuiKuanDanHao')"
                class="selWidth"
                size="small"
                clearable
              >
                <el-button slot="append" icon="el-icon-search" size="small" @click="seachList" />
              </el-input>
            </el-form-item>
            <!--<el-form-item class="width100">-->
            <!--<el-button size="small" @click="exports" v-hasPermi="['admin:export:excel:order']">导出</el-button>-->
            <!--</el-form-item>-->
          </el-form>
        </div>
      </div>
    </el-card>
    <div class="mt20">
      <!-- <cards-data :cardLists="cardLists" v-if="checkPermi(['admin:order:list:data'])"></cards-data> -->
    </div>
    <el-card class="box-card">
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        size="mini"
        class="table"
        highlight-current-row
        :header-cell-style="{ fontWeight: 'bold' }"
        :row-key="
          (row) => {
            return row.refundOrderNo;
          }
        "
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand demo-table-expands">
              <el-form-item :label="$t('tuiKuanShangPinZongShu') + '：'">
                <span>{{ props.row.totalNum }}</span>
              </el-form-item>
              <el-form-item :label="$t('pingTaiBeiZhu') + '：'">
                <span>{{ props.row.platformRemark | filterEmpty }}</span>
              </el-form-item>
              <el-form-item :label="$t('shangJiaBeiZhu') + '：'">
                <span>{{ props.row.merRemark | filterEmpty }}</span>
              </el-form-item>
              <el-form-item :label="$t('tuiKuanXinXi') + '：'">
                <div class="pup_card flex-column">
                  <span>{{ $t('tuiKuanYuanYin') }}：{{ props.row.refundReasonWap }}</span>
                  <span>{{ $t('beiZhuShuoMing') }}：{{ props.row.refundReasonWapExplain }}</span>
                  <!--<span>退款时间：{{scope.row.refundTime}}</span>-->
                  <span class="acea-row">
                    <span>{{ $t('tuiKuanPingZheng') }}：</span>
                    <template v-if="props.row.refundReasonWapImg">
                      <div
                        v-for="(item, index) in props.row.refundReasonWapImg.split(',')"
                        :key="index"
                        class="demo-image__preview"
                        style="width: 35px; height: auto; display: inline-block"
                      >
                        <el-image :src="item" :preview-src-list="[item]" />
                      </div>
                    </template>
                    <span v-else style="display: inline-block">{{ $t('wu') }}</span>
                  </span>
                </div>
                <div v-show="props.row.refundReason" class="pup_card flex-column">
                  <span>{{ $t('juJueYuanYin') }}：{{ props.row.refundReason }}</span>
                </div>
              </el-form-item>

              <!--<el-form-item v-if="props.row.status === 2" label="快递公司：">-->
              <!--<span>{{ props.row.delivery_type | filterEmpty }}</span>-->
              <!--</el-form-item>-->
              <!--<el-form-item v-if="props.row.status === 2" label="快递单号：">-->
              <!--<span class="mr5">{{ props.row.delivery_id | filterEmpty }}</span>-->
              <!--<el-button type="text" @click="openLogistics(props.row)">物流查询</el-button>-->
              <!--</el-form-item>-->
            </el-form>
          </template>
        </el-table-column>
        <el-table-column :label="$t('tuiKuanDanHao')" min-width="185" v-if="checkedCities.includes('tuiKuanDanHao')">
          <template slot-scope="scope">
            <span style="display: block" v-text="scope.row.refundOrderNo" />
          </template>
        </el-table-column>
        <el-table-column
          prop="merOrderNo"
          :label="$t('dingDanHao')"
          min-width="180"
          v-if="checkedCities.includes('dingDanHao')"
        />
        <el-table-column
          prop="realName"
          :label="$t('yongHuXinXi')"
          min-width="180"
          v-if="checkedCities.includes('yongHuXinXi')"
        />
        <el-table-column
          prop="email"
          :label="$t('yongHuYouXiang')"
          min-width="180"
          v-if="checkedCities.includes('yongHuYouXiang')"
        />
        <el-table-column
          prop="refundPrice"
          :label="$t('tuiKuanJinE')"
          min-width="100"
          v-if="checkedCities.includes('tuiKuanJinE')"
        />
        <el-table-column
          :label="$t('tuiKuanZhuangTai')"
          min-width="100"
          v-if="checkedCities.includes('tuiKuanZhuangTai')"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.refundStatus === 0 || scope.row.refundStatus === 1" class="refunding">
              <template>
                <el-popover trigger="hover" placement="left" :open-delay="500">
                  <b style="color: #f124c7; cursor: pointer" slot="reference">{{
                    scope.row.refundStatus | refundStatusFilter
                  }}</b>
                  <div class="pup_card flex-column">
                    <span>{{ $t('tuiKuanYuanYin') }}：{{ scope.row.refundReasonWap }}</span>
                    <span>{{ $t('beiZhuShuoMing') }}：{{ scope.row.refundReasonWapExplain }}</span>
                    <!--<span>退款时间：{{scope.row.refundTime}}</span>-->
                    <span class="acea-row">
                      {{ $t('tuiKuanPingZheng') }}：
                      <template v-if="scope.row.refundReasonWapImg">
                        <div
                          v-for="(item, index) in scope.row.refundReasonWapImg.split(',')"
                          :key="index"
                          class="demo-image__preview"
                          style="width: 35px; height: auto; display: inline-block"
                        >
                          <el-image :src="item" :preview-src-list="[item]" />
                        </div>
                      </template>
                      <span v-else style="display: inline-block">{{ $t('wu') }}</span>
                    </span>
                  </div>
                  <div class="pup_card flex-column">
                    <span>{{ $t('juJueYuanYin') }}：{{ scope.row.refundReason }}</span>
                  </div>
                </el-popover>
              </template>
            </div>
            <span v-else>{{ scope.row.refundStatus | refundStatusFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          :label="$t('chuangJianShiJian')"
          min-width="150"
          v-if="checkedCities.includes('chuangJianShiJian')"
        />
        <el-table-column
          :label="$t('caoZuo')"
          min-width="150"
          fixed="right"
          align="center"
          :render-header="renderHeader"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native="onOrderDetails(scope.row.merOrderNo)">{{
              $t('dingDanXiangQing')
            }}</el-button>
            <el-button
              type="text"
              size="small"
              @click.native="onOrderMark(scope.row)"
              v-hasPermi="['platform:refund:order:mark']"
              >{{ $t('dingDanBeiZhu') }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <div class="card_abs" v-show="card_select_show">
      <template>
        <div class="cell_ht">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange"
            >{{ $t('quanXuan') }}
          </el-checkbox>
          <el-button type="text" @click="checkSave()">{{ $t('baoCun') }}</el-button>
        </div>
        <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
          <el-checkbox v-for="item in columnData" :label="item" :key="item" class="check_cell">{{ item }}</el-checkbox>
        </el-checkbox-group>
      </template>
    </div>

    <!--详情-->
    <details-from ref="orderDetail" :orderId="orderId" />
  </div>
</template>

<script>
import { refundStatusNumApi, refundListApi, orderLogApi, refundMarkApi } from '@/api/order';
import detailsFrom from '@/components/orderDetail';
import { orderExcelApi } from '@/api/store';
import { checkPermi } from '@/utils/permission'; // 权限判断函数
export default {
  name: 'orderRefund',
  components: {
    detailsFrom,
  },
  data() {
    return {
      RefuseVisible: false,
      RefuseData: {},
      orderId: '',
      refundVisible: false,
      refundData: {},
      dialogVisibleJI: false,
      tableDataLog: {
        data: [],
        total: 0,
      },
      tableFromLog: {
        page: 1,
        limit: 10,
        orderNo: 0,
      },
      LogLoading: false,
      isCreate: 1,
      editData: null,
      dialogVisible: false,
      tableData: {
        data: [],
        total: 0,
      },
      listLoading: true,
      tableFrom: {
        refundStatus: '9',
        dateLimit: '',
        merOrderNo: '',
        refundOrderNo: '',
        page: 1,
        limit: 20,
      },
      orderChartType: {},
      timeVal: [],
      fromList: this.$constants.fromList,
      selectionList: [],
      ids: '',
      orderids: '',
      cardLists: [],
      proType: 0,
      active: false,
      card_select_show: false,
      checkAll: false,
      checkedCities: [
        'tuiKuanDanHao',
        'dingDanHao',
        'yongHuYouXiang',
        'yongHuXinXi',
        'tuiKuanJinE',
        'tuiKuanZhuangTai',
        'chuangJianShiJian',
      ],
      columnData: [
        'tuiKuanDanHao',
        'dingDanHao',
        'yongHuYouXiang',
        'yongHuXinXi',
        'tuiKuanJinE',
        'tuiKuanZhuangTai',
        'chuangJianShiJian',
      ],
      isIndeterminate: true,
    };
  },
  mounted() {
    this.getList();
    this.getOrderStatusNum();
    // this.getOrderListData();
  },
  methods: {
    checkPermi,
    resetFormRefundhandler() {
      this.refundVisible = false;
    },
    resetFormRefusehand() {
      this.RefuseVisible = false;
    },
    resetForm(formValue) {
      this.dialogVisible = false;
    },
    seachList() {
      this.tableFrom.page = 1;
      this.getList();
      this.getOrderStatusNum();
    },
    // 详情
    onOrderDetails(id) {
      this.orderId = id;
      this.$refs.orderDetail.getDetail(id);
      this.$refs.orderDetail.dialogVisible = true;
    },
    // 订单记录
    onOrderLog(id) {
      this.dialogVisibleJI = true;
      this.LogLoading = true;
      this.tableFromLog.orderNo = id;
      orderLogApi(this.tableFromLog)
        .then((res) => {
          this.tableDataLog.data = res.list;
          this.tableDataLog.total = res.total;
          this.LogLoading = false;
        })
        .catch(() => {
          this.LogLoading = false;
        });
    },
    pageChangeLog(page) {
      this.tableFromLog.page = page;
    },
    handleSizeChangeLog(val) {
      this.tableFromLog.limit = val;
    },
    handleClose() {
      this.dialogVisible = false;
    },
    // 备注
    onOrderMark(row) {
      this.$prompt(this.$t('tuiKuanDanBeiZhu'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        inputErrorMessage: this.$t('qingShuRuTuiKuanDanBeiZhu'),
        inputType: 'textarea',
        inputValue: row.platformRemark,
        inputPlaceholder: this.$t('qingShuRuTuiKuanDanBeiZhu'),
        inputValidator: (value) => {
          if (!value) return this.$t('shuRuBuNengWeiKong');
        },
      })
        .then(({ value }) => {
          refundMarkApi({ remark: value, refundOrderNo: row.refundOrderNo }).then(() => {
            this.$message.success(this.$t('caoZuoChengGong'));
            this.getList();
          });
        })
        .catch(() => {
          this.$message.info(this.$t('quXiaoShuRu'));
        });
    },
    handleSelectionChange(val) {
      this.selectionList = val;
      const data = [];
      this.selectionList.map((item) => {
        data.push(item.orderNo);
      });
      this.ids = data.join(',');
    },
    // 选择时间
    selectChange(tab) {
      this.timeVal = [];
      this.tableFrom.page = 1;
      this.getList();
      this.getOrderStatusNum();
      // this.getOrderListData();
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.dateLimit = e ? this.timeVal.join(',') : '';
      this.tableFrom.page = 1;
      this.getList();
      this.getOrderStatusNum();
      // this.getOrderListData();
    },
    // 列表
    getList() {
      this.listLoading = true;
      refundListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.list || [];
          this.tableData.total = res.total;
          this.listLoading = false;
          this.checkedCities = this.$cache.local.has('order_stroge')
            ? this.$cache.local.getJSON('order_stroge')
            : this.checkedCities;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    // 获取各状态数量
    getOrderStatusNum() {
      refundStatusNumApi({ dateLimit: this.tableFrom.dateLimit }).then((res) => {
        this.orderChartType = res;
      });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    exports() {
      let data = {
        dateLimit: this.tableFrom.dateLimit,
        orderNo: this.tableFrom.orderNo,
        refundStatus: this.tableFrom.status,
        type: this.tableFrom.type,
      };
      orderExcelApi(data).then((res) => {
        window.open(res.fileName);
      });
    },
    renderHeader(h) {
      return (
        <p>
          <span style="padding-right:5px;">{this.$t('caoZuo')}</span>
          <i class="el-icon-setting" onClick={() => this.handleAddItem()}></i>
        </p>
      );
    },
    handleAddItem() {
      if (this.card_select_show) {
        this.$set(this, 'card_select_show', false);
      } else if (!this.card_select_show) {
        this.$set(this, 'card_select_show', true);
      }
    },
    handleCheckAllChange(val) {
      this.checkedCities = val ? this.columnData : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.columnData.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.columnData.length;
    },
    checkSave() {
      this.$set(this, 'card_select_show', false);
      this.$modal.loading(this.$t('tishi14'));
      this.$cache.local.setJSON('order_stroge', this.checkedCities);
      setTimeout(this.$modal.closeLoading(), 1000);
    },
  },
};
</script>
<style lang="scss" scoped>
.red {
  color: #ed4014;
}
.el-table__body {
  width: 100%;
  table-layout: fixed !important;
}

.demo-table-expand {
  ::v-deeplabel {
    width: 83px !important;
  }
}

.refunding {
  span {
    display: block;
  }
}

.selWidth {
  width: 300px;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}

.el-icon-arrow-down {
  font-size: 12px;
}

.tabBox_tit {
  font-size: 12px !important;
  /*margin: 0 2px 0 10px;*/
  letter-spacing: 1px;
  /*padding: 5px 0;*/
  box-sizing: border-box;
}

.text_overflow {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 400px;
}

.pup_card {
  width: 200px;
  border-radius: 5px;
  padding: 5px;
  box-sizing: border-box;
  font-size: 12px;
  line-height: 16px;
  span {
    word-wrap: break-word;
    white-space: pre-wrap;
  }
}

.flex-column {
  display: flex;
  flex-direction: column;
}

.mt20 {
  margin-top: 20px;
}

.relative {
  position: relative;
}

.card_abs {
  position: absolute;
  padding-bottom: 15px;
  top: 340px;
  right: 40px;
  width: 200px;
  background: #fff;
  z-index: 99999;
  box-shadow: 0px 0px 14px 0px rgba(0, 0, 0, 0.1);
}

.cell_ht {
  height: 50px;
  padding: 15px 20px;
  box-sizing: border-box;
  border-bottom: 1px solid #eeeeee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.check_cell {
  width: 100%;
  padding: 15px 20px 0;
}

::v-deep .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #606266;
}
</style>
